package cn.jorian.framework.service;

import cn.jorian.framework.bean.DataBean;
import cn.jorian.framework.config.AppConfig;
import cn.jorian.framework.util.Base64Util;
import cn.jorian.framework.util.DateTimeUtil;
import cn.jorian.framework.util.GsonUtil;
import cn.jorian.framework.util.MD5Util;

import java.util.HashMap;
import java.util.Map;


public class ApiBaseService {

    /**
     * 请求参数
     */
    public Map<String, Object> getParam(DataBean<?> dataBean) {

        //appkey
        AppConfig appConfig = new AppConfig();
        appConfig.setAppKey("9476d5ce-fc6c-4ed6-a7fb-9a9d63f2d9f4");
        appConfig.setAppPassword("zmCoObWLQ6rH1nnGeh92F3et3RbvTAt1");
        //时间戳
        String timestamp = DateTimeUtil.getDateTime("yyyyMMddHHmmss");
        //data json
        String data = GsonUtil.toJson(dataBean);
        //data json base64编码
        String encodeData = Base64Util.encode(data);
        //签名 规则 md5(timestamp+data+AppPassword)
        String sign = MD5Util.md5Hex(timestamp + encodeData + appConfig.getAppPassword());
        //请求参数
        Map<String, Object> param = new HashMap<>();
        param.put("appkey", appConfig.getAppKey());
        param.put("timestamp", timestamp);
        param.put("sign", sign);
        param.put("ver", "1");
        param.put("data", encodeData);
        return param;
    }
}
